package com.qyb.boot.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.StringUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Properties;

/**
 * <p>
 * 实现功能：
 * <p/>
 *
 * @author 秦有宝
 * @version 1.0.00
 */
public class ServletApplication extends SpringBootServletInitializer {

    /**
     * 全局配置参数
     */
    public static volatile Properties properties;

    public static volatile String apiBaseAddress;

    private static final Logger LOGGER = LoggerFactory.getLogger(ServletApplication.class);

    /*
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);

        // 在springmvc中获取request的监听器
//        servletContext.addListener(RequestContextListener.class);
//        servletContext.addListener(IntrospectorCleanupListener.class);

        properties = new Properties();
        //读取配置文件
        Resource location =  new ClassPathResource("/application.properties");
        try {
            PropertiesLoaderUtils.fillProperties(properties, new EncodedResource(location, "UTF-8"));
            LOGGER.trace("已加载{}文件的配置。", location);
        } catch (IOException e) {
            LOGGER.trace("未加载{}文件的配置。", location);
        }
        String staticResourseCdn = properties.getProperty("STATIC_RESOURCE_CDN");
        if(!StringUtils.isEmpty(staticResourseCdn)){
            servletContext.setAttribute("STATIC_RESOURCE_CDN", staticResourseCdn);
        }
        String apiBaseAdd = properties.getProperty("API_BASE_ADDRESS");
        if(!StringUtils.isEmpty(apiBaseAdd)){
            apiBaseAddress = apiBaseAdd;
        }
    }
*/
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}
